package com.mcxx.modules.system.dto;

import cn.hutool.core.collection.CollectionUtil;
import lombok.Data;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;



@Data
public class VisitLimitDTO {
    String ip;
    String key;
    String userLoginId;
    String areaCode;
    Integer visitTimes;


    Date limitStartTime;


    int limitDate = 1;
    List<String> visitTimeList;

    public void addVisitTime(String visitTime) {
        if(CollectionUtil.isEmpty(visitTimeList)) {
            visitTimeList = new ArrayList<>();
        }
        visitTimeList.add(visitTime);
    }


    public boolean hasOutLimitDate() {
        if(limitDate == -1) {
            return false;
        }

        int diff = dateDiff();
        if(diff >= this.limitDate){
            return true;
        }
        return false;
    }

    public int dateDiff() {
        Calendar calSrc = Calendar.getInstance();
        calSrc.setTime(new Date());
        Calendar calDes = Calendar.getInstance();
        calDes.setTime(this.limitStartTime);
        long millisDiff = getMillis(calSrc) - getMillis(calDes);
        return (int) (millisDiff / (24 * 3600 * 1000));
    }

    public long getMillis(Calendar cal) {
        return cal.getTime().getTime();
    }
}
